package ptij.vocoder.baseModel.closed_loop;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.IntToken;
import ptolemy.data.Token;
import ptolemy.data.type.*;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// EncLag6
/**

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class EncLag6 extends TypedAtomicActor {

    public EncLag6(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        t0 = new TypedIOPort(this, "t0", true, false);
        t0.setTypeEquals(BaseType.OBJECT);

        t0_frac = new TypedIOPort(this, "t0_frac", true, false);
        t0_frac.setTypeEquals(BaseType.OBJECT);        
        
        t0_min = new TypedIOPort(this, "t0_min", true, false);
        t0_min.setTypeEquals(BaseType.OBJECT);

        t0_max = new TypedIOPort(this, "t0_max", true, false);
        t0_max.setTypeEquals(BaseType.OBJECT);

        index = new TypedIOPort(this, "index", false, true);
        index.setTypeEquals(BaseType.OBJECT);
        
        t0_frac_out = new TypedIOPort(this, "t0_frac_out", false, true);
        t0_frac_out.setTypeEquals(BaseType.OBJECT);
        
        t0_max_out = new TypedIOPort(this, "t0_max_out", false, true);
        t0_max_out.setTypeEquals(BaseType.OBJECT);
        
        t0_min_out = new TypedIOPort(this, "t0_min_out", false, true);
        t0_min_out.setTypeEquals(BaseType.OBJECT);
        
        txdtx_ctrl = new TypedIOPort(this, "txdtx_ctrl", true, false);
        txdtx_ctrl.setTypeEquals(BaseType.OBJECT);
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports
    public TypedIOPort t0;
    public TypedIOPort t0_frac;
    public TypedIOPort t0_min;
    public TypedIOPort t0_max;
    public TypedIOPort txdtx_ctrl;

    // Output Ports
    public TypedIOPort index;
    public TypedIOPort t0_frac_out;
    public TypedIOPort t0_min_out;
    public TypedIOPort t0_max_out;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
    }

    public void fire() throws IllegalActionException {
        ObjectToken obj = (ObjectToken) txdtx_ctrl.get(0);
        Short txdtx = (Short) obj.getValue();

        if((txdtx.shortValue() & Cnst.TX_SP_FLAG) != 0) {
            int pit = _which_subfr();;

            if(pit == 0) {
                obj = (ObjectToken) t0.get(0);
                Short T0 = (Short) obj.getValue();

                if(T0.shortValue() <= 0) {
                    obj = (ObjectToken) t0_frac.get(0);
                    Short T0_frac = (Short) obj.getValue();
                }
                else {
                    t0_frac_out.broadcast(new ObjectToken(new Short((short)0)));
                }
                
                short T0_min = BasicOp.sub(T0.shortValue(), (short)5);
                if(BasicOp.sub(T0_min, (short)Cnst.PIT_MIN) < 0) {
                    T0_min = Cnst.PIT_MIN;
                }
                short T0_max = BasicOp.add(T0_min, (short)9);

                if(BasicOp.sub(T0_max, (short)Cnst.PIT_MAX) > 0) {
                    T0_max = Cnst.PIT_MAX;
                    T0_min = BasicOp.sub(T0_max, (short)9);
                }

                t0_min_out.broadcast(new ObjectToken(new Short(T0_min)));
                t0_max_out.broadcast(new ObjectToken(new Short(T0_max)));
            }
            else {
                obj = (ObjectToken) t0.get(0);
                Short T0 = (Short) obj.getValue();

                obj = (ObjectToken) t0_min.get(0);
                Short T0_min = (Short) obj.getValue();

                obj = (ObjectToken) t0_frac.get(0);
                Short T0_frac = (Short) obj.getValue();

                index.broadcast(new ObjectToken(new Short((short)0)));
            }
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    private int _which_subfr() {
        return 0;
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

}
